package net.helix.element;

import org.lwjgl.util.vector.Matrix4f;
import org.lwjgl.util.vector.Vector3f;
import org.lwjgl.util.vector.Vector4f;

import net.helix.core.Bounds;
import net.helix.engine.library.items.LibraryItem;
import net.helix.engine.library.items.LibraryItem_Mesh;
import net.helix.physics.Collidable3d;
import net.helix.physics.Collision;

public class Element_Mesh extends ViewNodeElement implements Collidable3d {

	public Element_Mesh(LibraryItem item) {
		super(item);
	}

	@Override
	public void render() {
		item.render(properties);
		
	}
	
	public void renderSphere() {
		((LibraryItem_Mesh) item).renderSphere(properties);
		
	}
	public void renderCube() {
		((LibraryItem_Mesh) item).renderBoudingBox(properties);
		
	}
	
	public Bounds getBounds(){
		
		
		return ((LibraryItem_Mesh) item).getMesh().getBounds();
	}

	@Override
	public boolean lineHitTest(Vector3f rayPoint, Vector3f ray) {
		float[][] bounds = getBounds().getBoundsAsArray();
		for(int i=0;i<8;i++){
			Vector4f v = new Vector4f();
			v.x = bounds[i][0];
			v.y = bounds[i][1];
			v.z = bounds[i][2];
			v.w = 1;
			
			Matrix4f.transform(transform.matrix4f, v, v);
			
			bounds[i][0] = v.x;
			bounds[i][1] = v.y;
			bounds[i][2] = v.z;
			
		}
		
		if(Collision.rayIntersectCube(rayPoint, ray, bounds)){
			// TODO: check all triangles in mesh
			return true;
			
		}
		
		
		return false;
	}

	@Override
	public boolean sphereHitTest(Vector3f center, float radius) {
		// TODO Auto-generated method stub
		return false;
	}

}
